This Jupyter notebook is intented to be used alongside the book Python for Bioinformatics
Note: Before opening the file, this file should be accesible from this Jupyter notebook. In order to do so, the following commands will download these files from Github and extract them into a directory called samples.
In [ ]:
!wget https://github.com/Serulab/Py4Bio/archive/master.zip
!unzip master.zip
!mv Py4Bio-master/code/ch10/* ./
In [ ]:
!apt-get -y install apache2
In [ ]:
!pip install bottle
In [ ]:
!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip
Server for Listing 10.1
This command shows URL which is used for Listing 10.1
In [ ]:
get_ipython().system_raw('./ngrok http 80 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
In [ ]:
!/etc/init.d/apache2 restart
In [ ]:
!a2enmod cgid
In [ ]:
!service apache2 restart
Listing 10.1: firstcgi.py: First CGI script
In [ ]:
!cp firstcgi.py /usr/lib/cgi-bin/
!chmod 755 /usr/lib/cgi-bin/firstcgi.py
Access URL and put the /cgi-bin/firstcgi.py
Listing 10.2: greeting.html: HTML front end to send data to a CGI program
In [ ]:
!cp greeting.html /var/www/html
Listing 10.3: greeting.py: CGI program that processes the form in greeting.html.
In [ ]:
!cp greeting.py /usr/lib/cgi-bin/
!chmod 755 /usr/lib/cgi-bin/greeting.py
Access URL(same for Listing 10.1) and put the /greeting.html
Listing 10.4: protcharge.html: HTML front end to send data to a CGI program
In [ ]:
!cp protcharge.html /var/www/html
Listing 10.5: protcharge.py: Back-end code to calculate the net charge of a protein and proportion of charged amino acid
In [ ]:
%%writefile /usr/lib/cgi-bin/protcharge.py
#!/usr/bin/env python
import cgi, cgitb
def chargeandprop(aa_seq):
protseq = aa_seq.upper()
charge = -0.002
cp = 0
aa_charge = {'C':-.045,'D':-.999,'E':-.998,'H':.091,
'K':1,'R':1,'Y':-.001}
for aa in protseq:
charge += aa_charge.get(aa, 0)
if aa in aa_charge:
cp += 1
prop = float(cp)/len(aa_seq)*100
return (charge, prop)
cgitb.enable()
print('Content-Type: text/html\n')
form = cgi.FieldStorage()
seq = form.getvalue('aaseq', 'QWERTYYTREWQRTYEYTRQWE')
prop = form.getvalue('prop', 'n')
jobtitle = form.getvalue('title','No title')
charge, propvalue = chargeandprop(seq)
print('<html><body>Job title:{0}<br/>'.format(jobtitle))
print('Your sequence is:<br/>{0}<br/>'.format(seq))
print('Net charge: {0}<br/>'.format(charge))
if prop == 'y':
print('Proportion of charged AA: {0:.2f}<br/>'
.format(propvalue))
print('</body></html>')
In [ ]:
!chmod 755 /usr/lib/cgi-bin/protcharge.py
Access URL(same for Listing 10.1) and put the /protcharge.html
ex https://xxxxxx.ngrok.io/protcharge.html
Sample Data: MARLQTALLVVLVLLAVALQATEAGPYGANMEDSVCCRDYVRYRLPLRVVKHFYWTSDSCPRPGVVLLTFRDKEICADPRVPWVKMILNKLSQ
Stop Apache server
In [ ]:
!service apache2 stop
Stop ngrok
In [ ]:
!pkill ngrok
Server for Listing 10.6
This command shows URL which is used for Listing 10.6
In [ ]:
get_ipython().system_raw('./ngrok http 8000 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
Check URL is different from the first one(for Listing 10.1)
If the same URL, please stop ngrok
Listing 10.6: hellobottle.py: Hello World in Bottle
In [ ]:
!python helloworldbottle.py
Access URL for Listing 10.6
Stop the server Press stop botton 2 upper cell
Left of !python helloworldbottle.py
Successfully stopped there is ^C
Routes
In [ ]:
get_ipython().system_raw('./ngrok http 8000 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
In [ ]:
%%writefile helloworldbottle.py
from bottle import route, run
@route('/')
def index():
return 'Top level, or Index Page'
@route('/about')
def about():
return 'The about page'
run(host='localhost', port=8000)
In [ ]:
!python helloworldbottle.py
Access URL(for Routes) toppage and /about
Stop the server Press stop botton 2 upper cell
Left of !python helloworldbottle.py
Successfully stopped there is ^C
URL with Variable Parts
In [ ]:
get_ipython().system_raw('./ngrok http 8000 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
In [ ]:
%%writefile helloworldbottle.py
from bottle import route, run
@route('/')
def index():
return 'Top level, or Index Page'
@route('/about')
def about():
return 'The about page'
@route('/greets/<name>')
def shows_greeting(name):
return 'Hello {0}'.format(name)
run(host='localhost', port=8000)
In [ ]:
!python helloworldbottle.py
Access URL(for URL with Variable Parts) toppage and /about
Stop the server Press stop botton 2 upper cell
Left of !python helloworldbottle.py
Successfully stopped there is ^C
Listing 10.7: index.tpl: Template for Bottle with variables
Listing 10.8: indextemplate1.py: Bottle code for template with variables
In [ ]:
get_ipython().system_raw('./ngrok http 8000 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
In [ ]:
!python indextemplate1.py
Access URL(for URL Listing 10.7 and 10.8)
Stop the server Press stop botton 2 upper cell
Left of !python indextemplate1.py
Successfully stopped there is ^C
Listing 10.9: index2.tpl: Template for Bottle with variables and ow control
Listing 10.10: index2.py: Bottle code for template with variables
In [ ]:
get_ipython().system_raw('./ngrok http 8000 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
In [ ]:
!python indextemplate2.py
Access URL(for URL Listing 10.9 and 10.10)
Stop the server Press stop botton 2 upper cell
Left of !python indextemplate2.py
Successfully stopped there is ^C
Listing 10.11: indextemplate3.py: Bottle code with logic in code instead of in templates
Listing 10.12: index3.tpl: template for indextemplate3.py
In [ ]:
get_ipython().system_raw('./ngrok http 8000 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
In [ ]:
!python indextemplate3.py
Access URL(for URL Listing 10.11 and 10.12)
Stop the server Press stop botton 2 upper cell
Left of !python indextemplate3.py
Successfully stopped there is ^C
Listing 10.13: protchargebottle.py: Back-end of the program to calculate the net charge of a protein using Bottle
Listing 10.14: result.html: Template for showing the result of method protcharge
In [ ]:
get_ipython().system_raw('./ngrok http 8000 &')
In [ ]:
%%sh
curl -s http://localhost:4040/api/tunnels | python -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
In [ ]:
!python protchargebottle.py
Access URL(same for Listing 10.13 and 10.14)
Sample Data: MARLQTALLVVLVLLAVALQATEAGPYGANMEDSVCCRDYVRYRLPLRVVKHFYWTSDSCPRPGVVLLTFRDKEICADPRVPWVKMILNKLSQ
Stop the server Press stop botton 2 upper cell
Left of !python protchargebottle.py
Successfully stopped there is ^C